<%@ include file="/en/jsp/static.jsp" %>
<%@ page import="java.util.*,linuxbox.reservation.logic.*,linuxbox.util.StringUtil" %>

<%@ taglib uri="/linuxbox/common.tld" prefix="common" %>
<%@ taglib uri="/linuxbox/Reservation.tld" prefix="resv" %>
<%@ taglib uri="/linuxbox/Resource.tld" prefix="resource" %>

<%
  int ID = Integer.parseInt((String)request.getAttribute("ID"));
  String SUBJECT = (String)request.getAttribute("SUBJECT");
  int RESOURCEID = Integer.parseInt((String)request.getAttribute("RESOURCEID"));
  int ISALLDAY = Integer.parseInt((String)request.getAttribute("ISALLDAY"));
  int FROM_YEAR = Integer.parseInt((String)request.getAttribute("FROM_YEAR"));
  int FROM_MONTH = Integer.parseInt((String)request.getAttribute("FROM_MONTH"));
  int FROM_DAY = Integer.parseInt((String)request.getAttribute("FROM_DAY"));
  int FROM_HOUR = Integer.parseInt((String)request.getAttribute("FROM_HOUR"));
  int FROM_MINUTE = Integer.parseInt((String)request.getAttribute("FROM_MINUTE"));
  int TO_HOUR = Integer.parseInt((String)request.getAttribute("TO_HOUR"));
  int TO_MINUTE = Integer.parseInt((String)request.getAttribute("TO_MINUTE"));
  Calendar FROM_DATE = Calendar.getInstance(sec.getTimeZone());
  FROM_DATE.set(FROM_YEAR, FROM_MONTH, FROM_DAY, FROM_HOUR, FROM_MINUTE, 0);
  FROM_DATE.set(Calendar.MILLISECOND, 0);

  String DESCRIPTION = (String)request.getAttribute("DESCRIPTION");
  int REPEAT_TYPE = Integer.parseInt((String)request.getAttribute("REPEAT_TYPE"));
  int REPEAT_EVERYWEEK = Integer.parseInt((String)request.getAttribute("REPEAT_EVERYWEEK"));
  int REPEAT_WEEKDAYS = Integer.parseInt((String)request.getAttribute("REPEAT_WEEKDAYS"));
  int REPEAT_WHICHWEEK = Integer.parseInt((String)request.getAttribute("REPEAT_WHICHWEEK"));
  int REPEAT_WHICHWEEKDAY = Integer.parseInt((String)request.getAttribute("REPEAT_WHICHWEEKDAY"));
  int REPEAT_EVERYMONTH = Integer.parseInt((String)request.getAttribute("REPEAT_EVERYMONTH"));

//  int REPEAT_FOREVER = Integer.parseInt((String)request.getAttribute("REPEAT_FOREVER"));
  int UNTIL_YEAR = Integer.parseInt((String)request.getAttribute("UNTIL_YEAR"));
  int UNTIL_MONTH = Integer.parseInt((String)request.getAttribute("UNTIL_MONTH"));
  int UNTIL_DAY = Integer.parseInt((String)request.getAttribute("UNTIL_DAY"));

  long EDIT_DATE = FROM_DATE.getTime().getTime();
  Calendar currentDate = (Calendar)session.getAttribute("currentDate");
  if ( currentDate == null)
  {
    currentDate = Calendar.getInstance(sec.getTimeZone());
    session.setAttribute("currentDate",currentDate);
  }
  if (REPEAT_TYPE != 0)
  {
    EDIT_DATE = currentDate.getTime().getTime();
  }
  int timeInterval = sec.getConfigInt("calendar", "General", "Time Interval");
  int startHour = sec.getConfigInt("calendar", "General", "Working Hour Start");
  int endHour = sec.getConfigInt("calendar", "General", "Working Hour End");

  String ERROR_MSG = (String)request.getAttribute("ERROR_MSG");
  if (ERROR_MSG == null)
    ERROR_MSG = "";

//	var newwin=window.open(filename,'boxdaypicker', 'WIDTH=250,HEIGHT=227,RESIZABLE=yes,SCROLLBARS=no,STATUS=0');
%>

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--

  function DateSelector(dateMonth, dateYear, itemPrefix)
  {
    var filename = ('<%=BoxStatic.jspDir%>calendar/BoxPopupDayPicker.jsp?MONTH='+dateMonth+'&YEAR='+dateYear+'&ITEMPREFIX='+itemPrefix+'&SUBMIT=true');
    var newwin=window.open(filename,'boxdaypicker', 'WIDTH=210,HEIGHT=220,RESIZABLE=yes,SCROLLBARS=no,STATUS=0');
    if (newwin != null)
    {
      if (newwin.opener == null)
        newwin.opener = self;
    }
  }

  function onChangeResource()
  {
    document.theForm.submit();
  }
  function onChangeFromtime()
  {
    document.theForm.submit();
  }

  function onChangeCurrentDate(var t)
  {
    alert(t);
    document.theForm.t.value = t;
    document.theForm.submit();
  }

// -->
</SCRIPT><!-- jscript for  date picker -->

<jsp:include page="/en/jsp/header.jsp" flush="true"/>

<table width="100%" height=26 align=middle border=0 cellpadding=0 cellspacing=0 bgcolor="#f4f4f4" style="font-size:16px;color:#d05000">
<tr><td>
    &nbsp;<img src="<%=BoxStatic.imageDir%>itemball.jpg" width=14 height=15 border=0 align="absmiddle">
    <b><%=boxName%> - Resource Reservation Add/Edit</b>
</td></tr>
</table>

<hr width=\"100%\" color=#E06000 align=left>

<table width="100%" border=0 cellpadding=0 cellspacing=0>
<tr><td>

<center>

<form method="GET" name="theForm" action="<%=BoxStatic.servletDir%>BoxReservationAction">
<!-- when select resource, we need to refresh, default to Edit -->
<input type="hidden" name="SubmitReEdit" value="ReEdit">
<input type="hidden" name="t" value="<%=EDIT_DATE%>">

<table cellpadding="0" cellspacing="0" border="0" width="100%">
  <tr>
    <td valign="top">
      <table cellpadding="0" cellspacing="0" border="0" width="100%">
        <tr bgcolor="#f4f5e1">
          <td nowrap align="left"><b><font color=#ff0000><%=ERROR_MSG%></font></b></td>
        </tr>
        <tr bgcolor="#f4f5e1">
          <td nowrap align="left"><font size="-1" face="Arial,Helvetica">
            <input type="hidden" name="ID" value=<%=ID%>>
            <% if (ID == 0)
            { %>
            <input type="submit" name="SubmitSave" value="Save">
            <input type="submit" name="SubmitCancel" value="Cancel">
            <%
            }
            else
            {
            %>
            <input type="submit" name="SubmitUpdate" value="Update">
            <input type="submit" name="SubmitDelete" value="Delete" >
            <input type="submit" name="SubmitCancel" value="Cancel">
            <%}%>
            </font>
          </td>
        </tr>
      </table>

    <table cellpadding="0" cellspacing="0" border="0" width="100%">
      <tr bgcolor="#999999">
        <td>
          <table cellpadding="3" cellspacing="1" border="0" width="100%">
            <tr>
              <td align="left" bgcolor="#dcdcdc"><font face="Arial,Helvetica"><b>
                <% if (ID==0)
                    out.print("New ");
                   else
                    out.print("Edit ");
                %> Resource Reservation </b></font>
              </td>
            </tr>
            <tr>
              <td bgcolor="#eeeeee">
                <table cellpadding="3" cellspacing="0" border="0" width="100%">
                  <tr>
                    <td>
                      <b>Resource:</b>
                      <select name="RESOURCEID" size=1 onchange="onChangeResource()">
                        <resource:ResourceList selected="<%=RESOURCEID%>" allownull="false" />
                      </select>
                    </td>
                  </tr>
    <tr><td>
    	<table cellspacing=1 border=1 width="100%">
      	<tr><td>
      		<table cellspacing=0 border=0 width="100%">
            <tr>
<%
					{
  					Calendar tmpDay = (Calendar)currentDate.clone();
    				tmpDay.add(Calendar.DATE, -1);
    				long prevTime = tmpDay.getTime().getTime();
    				tmpDay.add(Calendar.DATE  , 2);
    				long nextTime = tmpDay.getTime().getTime();
%>
              <td width="1%" nowrap="nowrap">
                <a href="<%=BoxStatic.servletDir%>BoxReservationAction?SubmitView=view&t=<%=prevTime%>" onclick="onChangeCurrentDate(<%=prevTime%>)">
                  <img SRC="<%=BoxStatic.imageDir%>cal_left.gif" WIDTH="11" HEIGHT="11" BORDER="0">Previous</a>
              </td>
              <td width="1%" nowrap="nowrap">
                <font face="Arial,Helvetica" SIZE="+1"><b><%=StringUtil.getDateTimeFormatString(sec.getTimeZone(), BoxStatic.VERYLONG_DATE_FORMAT, currentDate.getTime())%></b></font>
              </td>
              <td nowrap="nowrap">
               <a href="<%=BoxStatic.servletDir%>BoxReservationAction?SubmitView=view&t=<%=nextTime%>"  onclick="onChangeCurrentDate(<%=nextTime%>)">Next<img
              SRC="<%=BoxStatic.imageDir%>cal_right.gif" WIDTH="11" HEIGHT="11" BORDER="0"></a>
              </td>

<%
					}
%>

      				<td width="1%" nowrap="nowrap" align=right bgcolor=#ddddff>Free</td>
              <td>&nbsp;</td>
              <td width="1%" nowrap="nowrap" align=right bgcolor=#ff0000>Busy</td>
            </tr>
          </table>
	      </td></tr>
      	<tr><td>
		    	<table cellspacing=1 border=1 width="100%">
			      <resv:ResourceTimeList timezone="<%=sec.getTimeZone()%>" timeInterval="<%=timeInterval%>" startHour="<%=startHour%>" endHour="<%=endHour%>"
      	      resourceID="<%=RESOURCEID%>" day="<%=EDIT_DATE%>" />
          </table>
    		</td></tr>
        <tr>
          <td valign="top" nowrap><font size="-1" face="Arial,Helvetica"><b>Subject:</b></font>
            <input name="SUBJECT" MAXLENGTH="80" SIZE="60" value="<%=SUBJECT%>">
          </td>
        </tr>
                  <tr>
                    <td valign="top" nowrap>
                      <b>Date:</b>
                      <select name="FROM_MONTH" size="1" onchange="onChangeFromtime()">
                        <common:MonthList selected="<%=FROM_MONTH%>" allownull="false" />
                      </select>
                      <select name="FROM_DAY" size="1" onchange="onChangeFromtime()">
                        <common:DayList selected="<%=FROM_DAY%>" allownull="false" />
                      </select>
                      <select name="FROM_YEAR" size="1" onchange="onChangeFromtime()">
                        <common:YearList selected="<%=FROM_YEAR%>" allownull="false" />
                      </select>
                      <A HREF="javascript:DateSelector(document.theForm.FROM_MONTH.options[document.theForm.FROM_MONTH.selectedIndex].value,
                      document.theForm.FROM_YEAR.options[document.theForm.FROM_YEAR.selectedIndex].value, 'FROM')"
                      ONMOUSEOVER="window.status='Click to select a date';return true"
                      ONMOUSEOUT="window.status='';return true;">
                      <IMG SRC="<%=BoxStatic.imageDir%>cal_daypicker.gif" WIDTH="22" HEIGHT="21" BORDER=0
                      alt="Click to select a date" ALIGN = "top"></A>
                    </td>
                  </tr>
                  <tr>
                    <td valign="top" nowrap>
                      <TABLE cellSpacing=0 cellPadding=3 border=0>
                        <TBODY>
                          <TR>
                            <TD vAlign=center>
                              <INPUT type=radio value=1 name="ISALLDAY"
                              <% if (ISALLDAY == 1 )
                                  out.println(" checked ");
                              %> >
                            </TD>
                            <TD vAlign=top>Reserve it <B>all day</B>.</TD>
                          </TR>
                          <TR>
                            <TD vAlign=center>
                              <INPUT type=radio value=0 name="ISALLDAY"
                                <% if (ISALLDAY == 0 )
                                  out.println(" checked ");
                                %>>
                            </TD>
                            <TD vAlign=top>
                              <b>From: </b>
                              <select  name="FROM_HOUR" size="1">
                                <common:HourList selected="<%=FROM_HOUR%>" allownull="false" />
                              </select>
                              <select name="FROM_MINUTE" size="1">
                                <common:MinuteList timeinterval="<%=timeInterval%>" selected="<%=FROM_MINUTE%>" allownull="false" />
                              </select>
                              <b>     To: </b>
                              <select  name="TO_HOUR" size="1">
                                <common:HourList selected="<%=TO_HOUR%>" allownull="false" />
                              </select>
                              <select name="TO_MINUTE" size="1">
                                <common:MinuteList timeinterval="<%=timeInterval%>" selected="<%=TO_MINUTE%>" allownull="false" />
                              </select>
                            </TD>
                          </TR>
                        </TBODY>
                      </TABLE>
                    </td>
                  </tr>
                    <td valign="top" nowrap>
                      <b>Description:</b><br>
                      <textarea name="DESCRIPTION" rows="4" wrap="virtual" cols="60"><%=DESCRIPTION%></textarea>
                    </td>
                  </tr>
                </table>
              </td>
            </tr>
            <tr>
              <td align="left" bgcolor="#dcdcdc">&nbsp; <font face="Arial,Helvetica"><b>Repeating </b></font></td>
            </tr>
            <tr>
              <td bgcolor="#eeeeee">


<TABLE CELLPADDING="3" CELLSPACING="0" WIDTH="100%" BORDER="0">
  <TR>
    <TD VALIGN="top" NOWRAP COLSPAN="2"><FONT size="-1">
      <INPUT TYPE="radio" NAME="REPEAT_TYPE" value="0"
      <% if (REPEAT_TYPE == 0 )
          out.println(" checked ");
      %>
      >&nbsp;Do not repeat this appointment.<BR><BR>
      <INPUT TYPE="radio" NAME="REPEAT_TYPE" value="1"
      <% if (REPEAT_TYPE == 1 )
          out.println(" checked ");
      %>
      >&nbsp;Repeat every
        <SELECT name="REPEAT_EVERYWEEK" ONCLICK="document.theForm.REPEAT_TYPE[1].checked='true'">
<%
  for(int i = 1; i < 5; i++)
  {
    out.print("<OPTION VALUE=\"");
    out.print(i);
    out.print("\"");
    if ( i == REPEAT_EVERYWEEK )
    {
      out.print(" selected ");
    }
    out.print(">");
    out.print(i);
    out.print("</OPTION>");
  }
%>
        </SELECT>
      week(s) on
    <INPUT TYPE="checkbox" name="REPEAT_WEEKDAYS" value="1" ONCLICK="document.theForm.REPEAT_TYPE[1].checked='true'"
    <%
    //1,2,4,8,16,32,64 = Sun,Mon,Tue,Wed,Thu,Fri,Sat
     if (( REPEAT_WEEKDAYS & 1) != 0)
        out.print(" checked ");
    %>
   >&nbsp;Su&nbsp;
    <INPUT TYPE="checkbox" name="REPEAT_WEEKDAYS" value="2" ONCLICK="document.theForm.REPEAT_TYPE[1].checked='true'"
    <%
     if (( REPEAT_WEEKDAYS & 2) != 0)
        out.print(" checked ");
    %>
   >&nbsp;Mo&nbsp;
    <INPUT TYPE="checkbox" name="REPEAT_WEEKDAYS" value="4" ONCLICK="document.theForm.REPEAT_TYPE[1].checked='true'"
    <%
     if (( REPEAT_WEEKDAYS & 4) != 0)
        out.print(" checked ");
    %>
   >&nbsp;Tu&nbsp;
    <INPUT TYPE="checkbox" name="REPEAT_WEEKDAYS" value="8" ONCLICK="document.theForm.REPEAT_TYPE[1].checked='true'"
    <%
     if (( REPEAT_WEEKDAYS & 8) != 0)
        out.print(" checked ");
    %>
   >&nbsp;We&nbsp;
    <INPUT TYPE="checkbox" name="REPEAT_WEEKDAYS" value="16" ONCLICK="document.theForm.REPEAT_TYPE[1].checked='true'"
    <%
     if (( REPEAT_WEEKDAYS & 16) != 0)
        out.print(" checked ");
    %>
   >&nbsp;Th&nbsp;
    <INPUT TYPE="checkbox" name="REPEAT_WEEKDAYS" value="32" ONCLICK="document.theForm.REPEAT_TYPE[1].checked='true'"
    <%
     if (( REPEAT_WEEKDAYS & 32) != 0)
        out.print(" checked ");
    %>
   >&nbsp;Fr&nbsp;
    <INPUT TYPE="checkbox" name="REPEAT_WEEKDAYS" value="64" ONCLICK="document.theForm.REPEAT_TYPE[1].checked='true'"
    <%
     if (( REPEAT_WEEKDAYS & 64) != 0)
        out.print(" checked ");
    %>
   >&nbsp;Sa&nbsp;
      <BR>
      <INPUT TYPE="radio" NAME="REPEAT_TYPE" value="2"
      <% if (REPEAT_TYPE == 2 )
          out.println(" checked ");
      %>
      >&nbsp;Repeat on the&nbsp;
      <SELECT name="REPEAT_WHICHWEEK" ONFOCUS="document.theForm.REPEAT_TYPE[2].checked='true'">
        <OPTION VALUE="1"  <% if (REPEAT_WHICHWEEK==1) out.print("selected");%> >First</OPTION>
        <OPTION VALUE="2"  <% if (REPEAT_WHICHWEEK==2) out.print("selected");%>>Second</OPTION>
        <OPTION VALUE="3"  <% if (REPEAT_WHICHWEEK==3) out.print("selected");%>>Third</OPTION>
        <OPTION VALUE="4"  <% if (REPEAT_WHICHWEEK==4) out.print("selected");%>>Fourth</OPTION>
        <OPTION VALUE="5"  <% if (REPEAT_WHICHWEEK==5) out.print("selected");%>>Last</OPTION>
      </SELECT>&nbsp; &nbsp;
      <SELECT name="REPEAT_WHICHWEEKDAY" ONFOCUS="document.theForm.REPEAT_TYPE[2].checked='true'">
        <OPTION VALUE="1"  <% if (REPEAT_WHICHWEEKDAY==1) out.print("selected");%> >Sunday</OPTION>
        <OPTION VALUE="2"  <% if (REPEAT_WHICHWEEKDAY==2) out.print("selected");%> >Monday</OPTION>
        <OPTION VALUE="4"  <% if (REPEAT_WHICHWEEKDAY==4) out.print("selected");%> >Tuesday</OPTION>
        <OPTION VALUE="8"  <% if (REPEAT_WHICHWEEKDAY==8) out.print("selected");%> >Wednesday</OPTION>
        <OPTION VALUE="16"  <% if (REPEAT_WHICHWEEKDAY==16) out.print("selected");%> >Thursday</OPTION>
        <OPTION VALUE="32"  <% if (REPEAT_WHICHWEEKDAY==32) out.print("selected");%> >Friday</OPTION>
        <OPTION VALUE="64"  <% if (REPEAT_WHICHWEEKDAY==64) out.print("selected");%> >Saturday</OPTION>
      </SELECT>&nbsp;of every&nbsp;
      <SELECT name="REPEAT_EVERYMONTH" ONFOCUS="document.theForm.REPEAT_TYPE[2].checked='true'">
        <OPTION VALUE="1" <% if (REPEAT_EVERYMONTH==1) out.print("selected");%> >1</OPTION>
        <OPTION VALUE="2" <% if (REPEAT_EVERYMONTH==2) out.print("selected");%>>2</OPTION>
        <OPTION VALUE="3" <% if (REPEAT_EVERYMONTH==3) out.print("selected");%>>3</OPTION>
        <OPTION VALUE="4" <% if (REPEAT_EVERYMONTH==4) out.print("selected");%>>4</OPTION>
        <OPTION VALUE="5" <% if (REPEAT_EVERYMONTH==5) out.print("selected");%>>5</OPTION>
        <OPTION VALUE="6" <% if (REPEAT_EVERYMONTH==6) out.print("selected");%>>6</OPTION>
      </SELECT>&nbsp;month(s)<BR>

      <INPUT TYPE="radio" NAME="REPEAT_TYPE" value="3"
      <% if (REPEAT_TYPE == 3 )
          out.println(" checked ");
      %>
      >&nbsp;Repeat every month<BR>
      <INPUT TYPE="radio" NAME="REPEAT_TYPE" value="4"
      <% if (REPEAT_TYPE == 4 )
          out.println(" checked ");
      %>
      >&nbsp;Repeat every year<BR>
      <BR>
      </FONT>
    </TD>
  </TR>
  <TR>
    <TD VALIGN="top" NOWRAP COLSPAN="2">
      Repeat until&nbsp;
      <SELECT name="UNTIL_MONTH">
        <common:MonthList selected="<%=UNTIL_MONTH%>" allownull="false" />
      </SELECT>
      <SELECT name="UNTIL_DAY">
        <common:DayList selected="<%=UNTIL_DAY%>" allownull="false" />
      </SELECT>
      <SELECT name="UNTIL_YEAR">
        <common:YearList selected="<%=UNTIL_YEAR%>" allownull="false" />
      </SELECT>
      <A HREF="javascript:DateSelector(document.theForm.UNTIL_MONTH.options[document.theForm.UNTIL_MONTH.selectedIndex].value,
      document.theForm.UNTIL_YEAR.options[document.theForm.UNTIL_YEAR.selectedIndex].value, 'UNTIL')"
      ONMOUSEOVER="window.status='Click to select a date';return true"
      ONMOUSEOUT="window.status='';return true;" >
      <IMG SRC="<%=BoxStatic.imageDir%>cal_daypicker.gif" WIDTH="22" HEIGHT="21" BORDER=0
      alt="Click to select a date" ALIGN = "top">
      </A>
    </TD>
  </TR>
</TABLE>

              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
    </td>
  </tr>
</table>



</center>
</form>

</td></tr>
</table>
<hr width=\"100%\" color=#E06000 align=left>

<jsp:include page="/en/jsp/footer.jsp" flush="true"/>
